package br.com.portal.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import br.com.portal.beans.Org;
import br.com.portal.factory.ConnectionFactory;

public class OrgDAO {
	public static Org getOrg(String orgId)
	{
		Org org = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		String query = "SELECT O.AD_CLIENT_ID, O.DESCRIPTION AS EMAIL, OI.EM_BRCORE_EMAIL AS EMAILPED, L.ADDRESS1||', '||L.ADDRESS2||' CEP '||L.POSTAL||' - '||R.NAME AS ENDERECO, OI.EM_BRCORE_FAX AS DESCMAX, O.AD_ORG_ID, O.NAME, OI.EM_BRCORE_PHONE, OI.EM_BRCORE_URL ";
			   query += "FROM AD_ORG O ";
			   query += "INNER JOIN AD_ORGINFO OI ON O.AD_ORG_ID=OI.AD_ORG_ID ";
			   query += "LEFT JOIN C_LOCATION L ON OI.C_LOCATION_ID=L.C_LOCATION_ID ";
			   query += "LEFT JOIN C_REGION R ON L.C_REGION_ID=R.C_REGION_ID ";
			   query += "WHERE O.AD_ORG_ID=? ";

			try {
				conn = ConnectionFactory.getConnection();
				stmt = conn.prepareStatement(query);
				stmt.setString(1,orgId);
				rs = stmt.executeQuery();
				if(rs.next()){
					org = new Org();
					org.setClientId(rs.getString(1));
					org.setEmail(rs.getString(2));
					org.setEmailPed(rs.getString(3));
					org.setEndereco(rs.getString(4));
					org.setDescMax(rs.getString(5));
					org.setId(rs.getString(6));
					org.setNome(rs.getString(7));
					org.setTelefone(rs.getString(8));
					org.setUrl(rs.getString(9));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		return org;
	}
}
